//
//  UIViewControllerExt_Transition.swift
//  Swift-app开发基础架构
//
//  Created by pro on 2017/8/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

import UIKit

private var animatorKey = "animatorKey"

extension UIViewController {

    
    /// 自定义的模态跳转动画 跳转方法
    ///
    /// - Parameters:
    ///   - vc: 跳转到的控制器
    ///   - animator: 自定义的动画管家 KZAnimator的子类
    func kz_present(to vc: UIViewController?, withAnimator animator: KZAnimator?) {
        
        if vc == nil { return }
        if animator == nil { return }

        vc!.transitioningDelegate = animator

        objc_setAssociatedObject(vc!, &animatorKey, animator, .OBJC_ASSOCIATION_RETAIN)
        vc!.kz_rigisterBackInteractive()
        
        self.present(vc!, animated: true, completion: nil)
    }

    
    /// 返回手势
    func kz_rigisterBackInteractive() {
        
        let back = KZInteractiveTransition()
        back.interactiveClouse = {
            self.dismiss(animated: true, completion: nil)
        }
        back.addGesture(toView: self.view)
        
        let obj = objc_getAssociatedObject(self, &animatorKey)
            
        if let animator = obj as? KZAnimator {
            animator.backInteractive = back
        }
    }
}
